package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.server.mapper.DepartmentMapper;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.mapper.EmployeeTransferMapper;
import com.xxxx.server.mapper.JoblevelMapper;
import com.xxxx.server.model.EmpMsg;
import com.xxxx.server.pojo.*;
import com.xxxx.server.query.EmpQuery;
import com.xxxx.server.rest.RespBean;
import com.xxxx.server.service.AmqpService;
import com.xxxx.server.service.IEmployeeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    //注入dao mapper
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private EmployeeTransferMapper employeeTransferMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private JoblevelMapper joblevelMapper;
    @Resource
    private AmqpService amqpService;


    @Override
    public Map<String, Object> selectEmpByParams(EmpQuery query) {
        Map<String,Object> map = new HashMap<>();

        //如果传name过来，那么将CurrentPage设置为1，从第一页开始显示。
        if(null != query.getName() && !"".equals(query.getName())){
            query.setCurrentPage(1);
        }

        //入职日期（老员工）
        if (query.getBeginDateScope() !=null && !"".equals(query.getBeginDateScope())){
            query.setBeginDate(query.getBeginDateScope().split(",")[0]);//这个是开始入职时间
            query.setEndDate(query.getBeginDateScope().split(",")[1]);//这个是入职结束时间
        }

        //开启分页
        PageHelper.startPage(query.getCurrentPage(),query.getSize());
        //通过条件查询员工列表
        List<EmpMsg> list = employeeMapper.selectEmpByParams(query);
        //得到分页数据
        PageInfo<EmpMsg> pageInfo = new PageInfo<>(list);

        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());//总记录数
        map.put("data",pageInfo.getList());//当前页显示的数据列表
        map.put("total",pageInfo.getTotal());
        return map;


    }

    //添加员工信息
    @Override
    public Integer insertEmp(Employee employee) {
        int insert = employeeMapper.insert(employee);
        System.out.println(employee.toString());
        amqpService.handler(employee);
        return insert;
    }

    //修改员工信息 根据id
    @Override
    public Integer updateEmp(Employee employee) {
        return employeeMapper.updateById(employee);
    }

    //删除员工信息 根据id
    @Override
    public Integer deleteEmp(Integer id) {
        return employeeMapper.deleteById(id);
    }

    @Override
    public Map<String, Object> selectEmpMaxWorkId() {
        Map<String, Object> map = new HashMap<>();
        String MaxWorkId = employeeMapper.selectEmpMaxWorkId();
        //转Integer类型，并＋1 留着添加员工使用生成新id使用
        Integer newWorkIdInt = Integer.valueOf(MaxWorkId) + 1;
        String newWorkId = newWorkIdInt.toString();
        for (int i = 0; i < MaxWorkId.length()-newWorkId.length(); i++) {
            newWorkId = "0" + newWorkId;//数据库员工id是string不是int，前面海油三个0
        }
        map.put("obj",newWorkId);
        return map;
    }

    public RespBean addETRecord(EmployeeTransfer et) {
        //
        employeeTransferMapper.addETRecord(et);
        //修改
        Department department = departmentMapper.selectDeptByName(et.getAfterDept());
        Joblevel joblevel = joblevelMapper.selectJoblevelByName(et.getAfterJoblevel());

        employeeMapper.updateByParams(et.getEmpId(),department.getId(),joblevel.getId());
        //
        return RespBean.success("添加成功！");
    }
}
